using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class TerrainHeight_1 : MonoBehaviour
{
	public GameObject TerrainParent;

	public List<Transform> terrains;

	public Rect[] terrainRects;

	public Transform cube;

	public TerrainHeight_1()
	{
		terrains = new List<Transform>();
	}

	public void Awake()
	{
		Transform[] componentsInChildren = TerrainParent.GetComponentsInChildren<Transform>();
		Terrain terrain = null;
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			terrain = (Terrain)componentsInChildren[i].GetComponent(typeof(Terrain));
			if ((bool)terrain)
			{
				terrains.Add(componentsInChildren[i]);
			}
		}
		terrainRects = new Rect[terrains.Count];
		for (int i = 0; i < terrains.Count; i++)
		{
			terrain = (Terrain)terrains[i].GetComponent(typeof(Terrain));
			ref Rect reference = ref terrainRects[i];
			Vector3 position = terrains[i].position;
			reference.x = position.x;
			ref Rect reference2 = ref terrainRects[i];
			Vector3 position2 = terrains[i].position;
			reference2.y = position2.z;
			ref Rect reference3 = ref terrainRects[i];
			Vector3 size = terrain.terrainData.size;
			reference3.width = size.x;
			ref Rect reference4 = ref terrainRects[i];
			Vector3 size2 = terrain.terrainData.size;
			reference4.height = size2.z;
		}
	}

	public Terrain GetTerrainTile(Vector3 position)
	{
		int num = 0;
		object result;
		while (true)
		{
			if (num < terrains.Count)
			{
				if (terrainRects[num].Contains(new Vector2(position.x, position.z)))
				{
					result = (Terrain)terrains[num].GetComponent(typeof(Terrain));
					break;
				}
				num++;
				continue;
			}
			result = null;
			break;
		}
		return (Terrain)result;
	}

	public float GetTerrainHeight(Vector3 position)
	{
		Terrain terrainTile = GetTerrainTile(position);
		return (!terrainTile) ? (-1f) : terrainTile.SampleHeight(position);
	}

	public void Update()
	{
		float terrainHeight = GetTerrainHeight(cube.position);
		UnityEngine.Debug.Log("Position: " + cube.position + " height: " + terrainHeight);
	}

	public void Main()
	{
	}
}
